home *** CD-ROM | disk | FTP | other *** search
/ C/C++ Users Group Library 1996 July / C-C++ Users Group Library July 1996.iso / vol_400 / 430_01 / m68kdis / main.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-12-29  |  6.6 KB  |  289 lines

  1. /*
  2.  *                 Author:  Christopher G. Phillips
  3.  *              Copyright (C) 1994 All Rights Reserved
  4.  *
  5.  *                              NOTICE
  6.  *
  7.  * Permission to use, copy, modify, and distribute this software and
  8.  * its documentation for any purpose and without fee is hereby granted
  9.  * provided that the above copyright notice appear in all copies and
  10.  * that both the copyright notice and this permission notice appear in
  11.  * supporting documentation.
  12.  *
  13.  * The author makes no representations about the suitability of this
  14.  * software for any purpose.  This software is provided ``as is''
  15.  * without express or implied warranty.
  16.  */
  17.  
  18. /*
  19.  * m68kdis' main (which processes command-line arguments) is here,
  20.  * along with definitions for most global variables.
  21.  */
  22.  
  23. #include <stdio.h>
  24. #include <stdlib.h>
  25. #include <string.h>
  26. #include <ctype.h>
  27. #include <setjmp.h>
  28. #include "dis.h"
  29.  
  30. static char    *patchlevel = "m68kdis 1.1";
  31.  
  32. char    *cc[] = {
  33.     "T", "F", "HI", "LS", "CC", "CS", "NE", "EQ",
  34.     "VC", "VS", "PL", "MI", "GE", "LT", "GT", "LE"
  35. };
  36. char    *bitd[] = {
  37.     "TST", "CHG", "CLR", "SET"
  38. };
  39. char    *bitf[] = {
  40.     "EXTU", "EXTS", "FFO", "INS"
  41. };
  42.  
  43. /*
  44.  * Buffers for printing instructions
  45.  */
  46. char        buf1[100];
  47. char        buf2[100];
  48. char        buf3[100];
  49.  
  50. int        pass;
  51. int        valid;
  52.  
  53. FILE        *infp;
  54. FILE        *outfp;
  55. m68kaddr    pc = 0;
  56. m68kaddr    ppc = 0;
  57. m68kaddr    initialpc = 0;
  58. int        chip = 0;
  59. int        lower = 0;
  60. int        minlen = 5;
  61. int        onepass = 0;
  62. int        sp = 0;
  63. int        odd = 0;
  64. int        linkfallthrough = 0;
  65. int        use_isprint = 0;
  66. int        fdigits = 6;
  67. size_t        slenprint = 30;
  68. #ifndef NOBAD
  69. int        dobad = 0;
  70. #endif
  71. char        *afile = NULL;
  72. char        *bfile = NULL;
  73. char        *ffile = NULL;
  74. char        *ifile = NULL;
  75. char        *jfile = NULL;
  76. char        *nfile = NULL;
  77. char        *nsfile = NULL;
  78.  
  79. jmp_buf        jmp;
  80. char        *sfile;
  81.  
  82. static char    *progname;
  83.  
  84. static void
  85. usage(void)
  86. {
  87.     fprintf(stderr,
  88. "Usage: %s [-00|-08|-10|-20|-30] [-a A-line-file] [-all[c]]\n", progname);
  89.     fprintf(stderr,
  90. "[-b break-file] [-f F-line-file] [-i instruction-file] [-isp] [-j jump-file]\n");
  91.     fprintf(stderr,
  92. "[-l] [-lft] [-n data-file] [-ns notstart-file] [-o output-file] [-odd]\n");
  93.     fprintf(stderr,
  94. "[-pc initialpc] [-s minlength] [-slenp maxlength] file...\n");
  95.     exit(1);
  96. }
  97.  
  98. int
  99. main(int argc, char **argv)
  100. {
  101.     int    status = 0;
  102.     char    *ofile = NULL;
  103.  
  104.     progname = argv[0];
  105.  
  106.     while (--argc && **++argv == '-') {
  107.         if (strcmp("-pc", *argv) == 0 && argc--) {
  108.             initialpc = strtoul(*++argv, NULL, 0);
  109.         } else if (strcmp("-000", *argv) == 0) {
  110.             chip |= MC68000;
  111.         } else if (strcmp("-008", *argv) == 0) {
  112.             chip |= MC68008;
  113.         } else if (strcmp("-010", *argv) == 0) {
  114.             chip |= MC68010;
  115.         } else if (strcmp("-020", *argv) == 0) {
  116.             chip |= MC68020;
  117.         } else if (strcmp("-030", *argv) == 0) {
  118.             chip |= MC68030;
  119.         } else if (strcmp("-040", *argv) == 0) {
  120.             chip |= MC68040;
  121.         } else if (strcmp("-851", *argv) == 0) {
  122.             chip |= MC68851;
  123.         } else if (strcmp("-881", *argv) == 0) {
  124.             chip |= MC68881;
  125.         } else if (strcmp("-882", *argv) == 0) {
  126.             chip |= MC68882;
  127.         } else if (strcmp("-o", *argv) == 0 && argc--) {
  128.             /*
  129.              * output pathname
  130.              */
  131.             ofile = *++argv;
  132.         } else if (strcmp("-i", *argv) == 0 && argc--) {
  133.             /*
  134.              * file containing offsets
  135.              * that *are* instructions
  136.              */
  137.             ifile = *++argv;
  138.         } else if (strcmp("-a", *argv) == 0 && argc--) {
  139.             /*
  140.              * file containing valid A-line (1010) instructions
  141.              */
  142.             afile = *++argv;
  143.         } else if (strcmp("-b", *argv) == 0 && argc--) {
  144.             /*
  145.              * file containing offsets of data for which
  146.              * a new line of output should be started
  147.              */
  148.             bfile = *++argv;
  149.         } else if (strcmp("-f", *argv) == 0 && argc--) {
  150.             /*
  151.              * file containing valid F-line (1111) instructions
  152.              */
  153.             ffile = *++argv;
  154.         } else if (strcmp("-j", *argv) == 0 && argc--) {
  155.             /*
  156.              * file containing A-line and F-line
  157.              * instructions that cause PC to be changed such
  158.              * that it is not necessary for the next word to
  159.              * be an instruction
  160.              */
  161.             jfile = *++argv;
  162.         } else if (strcmp("-n", *argv) == 0 && argc--) {
  163.             /*
  164.              * file containing offsets that are *not* instructions
  165.              */
  166.             nfile = *++argv;
  167.         } else if (strcmp("-ns", *argv) == 0 && argc--) {
  168.             /*
  169.              * file containing offsets that
  170.              * are not the *start* of instructions
  171.              */
  172.             nsfile = *++argv;
  173.         } else if (strcmp("-all", *argv) == 0)
  174.             onepass = INCONSISTENT;
  175.         else if (strcmp("-allc", *argv) == 0)
  176.             onepass = CONSISTENT;
  177.         else if (strcmp("-isp", *argv) == 0)
  178.             use_isprint = 1;
  179.         else if (strcmp("-lft", *argv) == 0)
  180.             linkfallthrough = 1;
  181.         else if (strcmp("-odd", *argv) == 0)
  182.             odd = 1;
  183.         else if (strcmp("-sp", *argv) == 0)
  184.             sp = 1;
  185.         else if (strcmp("-l", *argv) == 0) {
  186.             lower = 1;
  187. #ifdef DEBUG
  188.         } else if (strncmp("-d", *argv, 2) == 0) {
  189.             extern int    debug;
  190.  
  191.             if (isdigit(argv[0][2]))
  192.                 debug = atoi(&argv[0][2]);
  193. #endif
  194. #ifndef NOBAD
  195.         } else if (strcmp("-bad", *argv) == 0) {
  196.             dobad = 1;
  197. #endif
  198.         } else if (strcmp("-s", *argv) == 0 && argc--) {
  199.             minlen = atoi(*++argv);
  200.             if (minlen < 2)
  201.                 minlen = 2;
  202.         } else if (strcmp("-fdigits", *argv) == 0 && argc--) {
  203.             fdigits = atoi(*++argv);
  204.             if (fdigits < 2)
  205.                 fdigits = 2;
  206.         } else if (strcmp("-slenp", *argv) == 0 && argc--) {
  207.             slenprint = atoi(*++argv);
  208.             if (slenprint < 10)
  209.                 slenprint = 10;
  210.         } else {
  211.             fprintf(stderr, "%s: bad option: %s\n", progname,
  212.               *argv);
  213.             usage();
  214.         }
  215.     }
  216.  
  217.     if (!odd && initialpc & 1) {
  218.         fprintf(stderr, "%s: initialpc odd but -odd not specified\n",
  219.           progname);
  220.         exit(1);
  221.     }
  222.  
  223.     if (!CPU(chip))
  224.         chip |= MC68000;
  225.  
  226.     if (PMMU(chip) && CPU(chip) < MC68020) {
  227.         fprintf(stderr, "%s: bad cpu/coprocessor combination\n",
  228.           progname);
  229.         exit(1);
  230.     }
  231.  
  232.     if (argc == 0 && onepass) {
  233.         infp = stdin;
  234.         sfile = "stdin";
  235.         if (setjmp(jmp) == 0)
  236.             disassemble();
  237.     } else if (argc == 1 || argc > 1 && !ofile) {
  238.         argv--;
  239.         while (argc--) {
  240.             char    *lastslash;
  241.             size_t    len;
  242.             size_t    extra;
  243.         
  244.             if ((infp = fopen(*++argv, "r")) == NULL) {
  245.                 perror(*argv);
  246.                 status++;
  247.                 continue;
  248.             }
  249.  
  250.             /*
  251.              * Determine output filename.
  252.              * If unspecified, add ".s" to end of input filename.
  253.              */
  254.             if (ofile)
  255.                 sfile = ofile;
  256.             else {
  257.                 if (lastslash = strrchr(*argv, '/'))
  258.                     *argv = lastslash + 1;
  259.                 len = strlen(*argv);
  260.                 extra = (len > 2 && argv[0][len - 2] == '.'
  261.                   && argv[0][len - 1] == 'o') ? 0 : 2;
  262.                 if ((sfile = malloc(len + extra + 1)) == NULL) {
  263.                     perror(*argv);
  264.                     status++;
  265.                     (void)fclose(infp);
  266.                     continue;
  267.                 }
  268.                 strcpy(sfile, *argv);
  269.                 strcpy(&sfile[len - 2 + extra], ".s");
  270.             }
  271.             if ((outfp = fopen(sfile, "w")) == NULL) {
  272.                 perror(sfile);
  273.                 status++;
  274.                 (void)fclose(infp);
  275.                 continue;
  276.             }
  277.             if (setjmp(jmp) == 0)
  278.                 disassemble();
  279.             (void)fclose(infp);
  280.             (void)fclose(outfp);
  281.             if (!ofile)
  282.                 free(sfile);
  283.         }
  284.     } else
  285.         usage();
  286.  
  287.     exit(status);
  288. }
  289.